﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace NursingFormSystem.Common
{
    public static class DateTimeTran
    {
        /// <summary>
        /// 转换为时间格式，转换失败时：0 返回null, 1 返回当前时间
        /// </summary>
        /// <param name="default">转换失败时返回的值：0 返回null, 1 返回当前时间</param>
        public static System.DateTime? ToIsNULLDateTime1(this object self, int def = 0)
        {
            System.DateTime? rst = null;
            if (self == null)
            {
                if (def == 0) return null;
                else return System.DateTime.Now;
            }
            try
            {
                rst = System.DateTime.Parse(self.ToString());
            }
            catch (Exception)
            {
                if (def == 0) return null;
                else return System.DateTime.Now;
            }
            return rst;
        }


        /// <summary>
        /// 转换为时间格式，转换失败时返回的值：1900-1-1
        /// </summary>
        /// <param name="default">转换失败时返回的值：1900-1-1</param>
        public static System.DateTime ToIsNULLDateTime2(this object self)
        {
            System.DateTime rst = System.DateTime.Parse("1900-1-1");
            if (self == null)
            {
                return rst;
            }
            try
            {
                rst = System.DateTime.Parse(self.ToString());
            }
            catch (Exception)
            {
                return rst;
            }
            return rst;
        }

        /// <summary>
        /// 是否为时间型字符串
        /// </summary>
        /// <param name="source">时间字符串(15:00:00)</param>
        /// <returns></returns>
        public static bool IsTime(string StrSource)
        {
            return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }

        /// <summary>
        /// 将时分秒 没有补零的自动补0
        /// 传入格式 时:分 或 时:分:秒
        /// 不符合规则直接返回string.Empty;
        /// </summary>
        /// <param name="time">返回HH:mm or HH:mm:ss</param>
        public static string TimeFillZero(string time)
        {
            string res = string.Empty;
            if (string.IsNullOrEmpty(time)) return res;
            if (!time.Contains(":")) return res;
            string[] hms = time.Split(':');
            foreach (var item in hms)
            {
                if (string.IsNullOrEmpty(item)) return res;
                if (!System.Text.RegularExpressions.Regex.IsMatch(item, @"^\d+$")) return res;
            }
            int hh = Convert.ToInt32(hms[0]);
            if (hh < 0 || hh >= 24) return res;
            int mm = Convert.ToInt32(hms[1]);
            if (mm < 0 || mm >= 60) return res;
            int ss = (hms.Length >= 3) ? Convert.ToInt32(hms[2]) : 0;
            if (ss < 0 || ss >= 60) return res;
            var now = DateTime.Now;
            string timeFormart = hms.Length >= 3 ? "HH:mm:ss" : "HH:mm";
            DateTime datetime = new DateTime(now.Year, now.Month, now.Day, hh, mm, ss);
            res = datetime.ToString(timeFormart);
            return res;
        }
    }
}
